layui.use(['table', 'layer', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;
    var layer = layui.layer;

    /**
     * 弹窗广告管理
     */
    var NanPopupAdvert = {
        tableId: "nanPopupAdvertTable"
    };

    /**
     * 初始化表格的列
     */
    NanPopupAdvert.initColumn = function () {
        return [[
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 220},
            {field: 'id', sort: true, title: 'ID', width: 80},
            {field: 'name', sort: false, title: '广告名称'},
            {field: 'typeStr', sort: false, title: '广告类型', width: 150},
            {field: 'deviceName', sort: false, title: '设备', width: 150},
            {field: 'statusStr', sort: false, title: '状态', width: 100},
            {field: 'sort', sort: true, title: '排序', width: 80},
            {field: 'createTime', sort: false, title: '创建时间', width: 180}
        ]];
    };

    /**
     * 点击查询按钮
     */
    NanPopupAdvert.search = function () {
        var queryData = {};

        queryData['name'] = $('#name').val();
        queryData['type'] = $('#type').val();
        queryData['status'] = $('#status').val();

        table.reload(NanPopupAdvert.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    NanPopupAdvert.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/nanPopupAdvert/add'
    };

    /**
     * 跳转到编辑页面
     *
     * @param data 点击按钮时候的行数据
     */
    NanPopupAdvert.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/nanPopupAdvert/edit?id=' + data.id
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    NanPopupAdvert.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/nanPopupAdvert/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(NanPopupAdvert.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除该广告?", operation);
    };

    /**
     * 立即发送
     *
     * @param data 点击按钮时候的行数据
     */
    NanPopupAdvert.onSendNow = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/nanPopupAdvert/sendNow", function (result) {
                Feng.success("发送成功!");
                console.log("广告信息:", result.data);
            }, function (data) {
                Feng.error("发送失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("确认立即发送该广告?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + NanPopupAdvert.tableId,
        url: Feng.ctxPath + '/nanPopupAdvert/list',
        page: true,
        limit: 20,
        height: "full-158",
        cellMinWidth: 100,
        cols: NanPopupAdvert.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        NanPopupAdvert.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        NanPopupAdvert.jumpAddPage();
    });

    // 排序事件
    table.on('sort(' + NanPopupAdvert.tableId + ')', function (obj) {
        table.reload(NanPopupAdvert.tableId, {
            initSort: obj,
            where: {
                field: obj.field.replace(/([A-Z])/g, "_$1").toLowerCase(),
                order: obj.type
            }
        });
    });

    // 工具条点击事件
    table.on('tool(' + NanPopupAdvert.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            NanPopupAdvert.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            NanPopupAdvert.onDeleteItem(data);
        } else if (layEvent === 'sendNow') {
            NanPopupAdvert.onSendNow(data);
        }
    });
});

